﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace StringSearch
{
    class HorspoolMatcher : Matcher
    {
        private int MAX_SYMBOLS = 256;
        private int[] _delta;
//         private string _pattern;
//         private ArrayList _founds;
// 
//         public ArrayList Founds
//         {
//             get
//             {
//                 return this._founds;
//             }
//         }

        private HorspoolMatcher()
        {
        }
        
        public HorspoolMatcher(string[] _patterns)
        {
            this._patterns = _patterns;
            this._delta = new int[MAX_SYMBOLS];
            for (int i = 0; i < MAX_SYMBOLS; i++)
            {
                this._delta[i] = this._patterns[0].Length;
            }
            for (int i = 0; i < this._patterns[0].Length - 1; i++)
            {
                this._delta[Convert.ToInt16(this._patterns[0][i])] = i;
            }
            this._founds = null;
        }

        public override void Search(string str)
        {
            int j = this._patterns[0].Length - 1;
            int t = this._delta[Convert.ToInt16(this._patterns[0][j])];
            this._delta[Convert.ToInt16(this._patterns[0][j])] = 0;
            while (j < str.Length)
            {
                int s = this._delta[Convert.ToInt16(str[j])];
                if (s != 0)
                {
                    j += s;
                }
                else
                {
                    string str1 = str.Substring(j - this._patterns[0].Length + 1, this._patterns[0].Length - 1);
                    string str2 = this._patterns[0].Substring(0, this._patterns[0].Length - 1);
                    if (str1 == str2)
                    {
						if (this._founds == null)
						{
							this._founds = new Dictionary<int, List<int>>();
							this._founds.Add(0, new List<int>());
						}
						this._founds[0].Add(j);
                    }
                    j += t;
                }
            }
        }
    }
}
